<?php 
	if($_GET['nama'] == ''){
		$id = $_GET['id']; 
	}else{
		$nama = str_replace("-"," ","$_GET[nama]");
		$penduduk = mysql_query("select id_penduduk from penduduk where nama = '$nama' and tgl_lahir = '$_GET[tgl]'");
		$p = mysql_fetch_array($penduduk);
		$id = $p['id_penduduk'];
	}

	$sql="select id_penduduk, nama, tempat_lahir, tgl_lahir from penduduk where id_penduduk='$id'";
	$qry=mysql_query($sql);
	$hasil = mysql_fetch_array($qry);
	
	
	//Penomoran otomatis
	$tgl = date("my01");
	$query = "SELECT max(id_peserta) as maxID FROM peserta where id_peserta like '$tgl%'";
	$kode = mysql_query($query);
	$data = mysql_fetch_array($kode);
	$idMax = $data['maxID'];
	$noUrut = (int) substr($idMax, 6, 4);
	$noUrut++;
	$newID = "$tgl".sprintf("%04s", $noUrut);
?>

<script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
</script>

<div id=tiga_ba>
	<div id=tiga_baa>Tambah Peserta</div>
	<div id=tiga_bab></div>
	<div class=clear></div>
</div>
<script type="text/javascript" src="asset/js/jpegcam/webcam.js"></script>
<script type="text/javascript">
webcam.set_api_url( 'module/mod_peserta/js/ambil_gambar.php' );
//webcam.set_api_url( 'test.php' );
		webcam.set_quality( 100 ); // JPEG quality (1 - 100)
		webcam.set_shutter_sound( true ); // play shutter click sound
</script>
<script type="text/javascript">
webcam.set_hook( 'onComplete', 'my_completion_handler' );
		
		function take_snapshot() {
			// take snapshot and upload to server
			document.getElementById('upload_results').innerHTML = '<h1>Ambil Gambar</h1>';
			webcam.snap();
		}
		
		function my_completion_handler(msg) {
			// extract URL out of PHP output
			if (msg.match(/(http\:\/\/\S+)/)) {
				var image_url = RegExp.$1;
				// show JPEG image in page
				//alert(image_url);
				document.getElementById('upload_results').innerHTML = 
					'<img src="' + image_url + '">';
				
				// reset camera for another shot
				$("#cek_foto").val('1');
				webcam.reset();
			}
			else alert("PHP Error: " + msg);
		}
</script>
<form action="utama.php?module=mod_peserta&act=simpan_peserta" onsubmit="return gsValidate('id|Jenis|surat','ID Peserta Harus Diisi|Jenis Peserta Harus Dipilih|Nomer Surat Harus Diisi','required|required|required');" method="post" enctype="multipart/form-data">
	<table style="margin: 10px 15px 10px 15px;">
		<tr>
			<td width="90">ID Peserta</td>
			<td width="250">
				<input name="kd" type="text" id="kd" class="input2" value="<?php echo $newID; ?>" disabled/>
				<input name="kode" type="hidden" id="kode" class="input2" value="<?php echo $newID; ?>"/>
				<input name="id" type="hidden" id="id" value="<? echo $hasil['id_penduduk']; ?>" class="input2"/>
			</td>
			<td rowspan="6">
				<div id="kotak_foto">
					<script type="text/javascript">
					document.write( webcam.get_html(230, 250) );
					</script>
				</div>
			</td>
			<td rowspan="6"><div id="upload_results"></div></td>
		</tr>
		<tr>
			<td>Nama</td>
			<td width="300">
				<input name="nama" type="text" id="nama" value="<? echo $hasil['nama']; ?>" class="input2" disabled/></td>
		</tr>
		<tr>
			<td>Tempat Lahir</td>
			<td width="300">
				<input name="tempat_lahir" type="text" id="tempat_lahir" value="<? echo $hasil['tempat_lahir']; ?>" class="input2" disabled/>			</td>
		</tr>
		<tr>
			<td>Tanggal Lahir</td>
			<td width="300">
				<input name="tgl_lahir" type="text" id="tgl_lahir" value="<? echo tgl_ind1("$hasil[tgl_lahir]"); ?>" class="input2" disabled/>			</td>
		</tr>
		<tr>
			<td>Jenis Peserta</td>
			<td width="300">
				<?php echo buat_kombo("jenis_peserta","Jenis","input2","id_jenis_peserta"); ?>
				<span class="red">*</span>
			</td>
		</tr>
		<tr>
			<td>No Surat</td>
			<td width="300">
				<input name="surat" type="text" id="surat" class="input2"/>
			</td>
		</tr>
		<tr>
			<td>Tanggal Masuk </td>
			<td>
				<input name="masa_berlaku" type="text" id="datepicker" class="input2" value="<?php echo date('m/d/Y');?>" />
			</td>
			<td colspan="2" align="center">
				<input type="button" value="Setting" class="button" onClick="webcam.configure()">
				&nbsp;&nbsp;
				<input type=button value="Capture" class="button" onClick="take_snapshot()">
			</td>
		</tr>
		<tr>
		  <td>Upload Foto </td>
		  <td><input type="file" name="foto"></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input name="submit" type="submit" class="button" value="Simpan" /></td>
		</tr>
	</table>                
</form>